package com.handinfo.android.uicontrols.controls;

import android.graphics.Bitmap;
import android.graphics.PointF;

import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.utils.Tools;

public class DWBackground extends DWControl
{
//	private Bitmap m_corner = null;
//	private Bitmap m_side = null;
	private int m_background_color = 0x88039AAE;//背景色
	private int m_background_fill = 0xFF039AAE;//背景色
	private boolean m_is_corner = true;
	private Bitmap img_corner_lu = null;
	private Bitmap img_corner_ld = null;
	private Bitmap img_corner_rd = null;
	private Bitmap img_corner_ru = null;
	
	
	private boolean m_is_fill = false;
	
	
	public DWBackground(int w, int h) {
		this(null, w, h);
	}
	
	public DWBackground(int x, int y, int w, int h) {
		this(null, x, y, w, h);
	}
	
	public DWBackground(Bitmap img, int w, int h) {
		this(img, 0, 0, w, h);
	}
	
	public DWBackground(Bitmap img, int x, int y, int w, int h) {
		setBackground(img);
		setPoint(x, y);
		setShowWideHigh(w, h);
	}
	/**
	 * 设置不透明的颜色
	 * @param color
	 */
	public void setFillColor(int color) {
		m_background_fill = color;
	}
	
	/**
	 * 设置是否半透
	 * @param fill false半透, true不透明
	 */
	public void setFill(boolean fill) {
		m_is_fill = fill;
	}
	
	/**
	 * 是否显示四角
	 * @param show
	 */
	public void setShowCorner(boolean show) {
		m_is_corner = show;
	}
	
	public void setBackground(Bitmap img) {
		img_background = img;
		if (img == null) {
			img_corner_lu = DWControlsManager.getInstance().createControlImage(DWControlsManager.RES_CORNER_LU);
			img_corner_ld = DWControlsManager.getInstance().createControlImage(DWControlsManager.RES_CORNER_LD);
			img_corner_rd = DWControlsManager.getInstance().createControlImage(DWControlsManager.RES_CORNER_RD);
			img_corner_ru = DWControlsManager.getInstance().createControlImage(DWControlsManager.RES_CORNER_RU);
		} else {
			img_corner_lu = null;
			img_corner_ld = null;
			img_corner_rd = null;
			img_corner_ru = null;
		}
	}

//	/**
//	 * 创建一个图块拼接的背景
//	 * @param corner 左上角
//	 * @param side 上边
//	 * @param w
//	 * @param h
//	 */
//	public DWBackground(Bitmap corner, Bitmap side, int w, int h)
//	{
//		this(corner, side, 0, 0, w, h);
//	}
//
//	/**
//	 * 创建一个图块拼接的背景
//	 * @param corner 左上角
//	 * @param side 上边
//	 * @param x
//	 * @param y
//	 * @param w
//	 * @param h
//	 */
//	public DWBackground(Bitmap corner, Bitmap side, int x, int y, int w, int h)
//	{
//		if (corner == null) {
//			throw new NullPointerException("DWBackground corner is null!");
//		}
//		if (side == null) {
//			throw new NullPointerException("DWBackground side is null!");
//		}
//		m_corner = corner;
//		m_side = side;
//		m_background_color = m_corner.getPixel(m_corner.getWidth() - 1, m_corner.getHeight() - 1);
//		setPoint(x, y);
//		setShowWideHigh(w, h);
//	}

	public void render(DWGraphics g)
	{
		if (m_is_show) {
			setClip(g);
			if (img_background != null) {
				g.drawBitmap(img_background, m_show_x, m_show_y, m_show_w, m_show_h, Tools.LEFT_TOP);
				renderRect(g);
			} else {
				drawFourCorner(g);
				renderRect(g);
			}
		}
	}

	public void renderLimits(DWGraphics g, int x, int y, int w, int h, int offsetx, int offsety)
	{
		if (m_is_show) {
			setSubClip(g, x, y, w, h, offsetx, offsety);
			if (img_background != null) {
				g.drawBitmap(img_background, x + m_show_x - offsetx, y + m_show_y - offsety, m_show_w, m_show_h, Tools.LEFT_TOP);
			} else {
				drawFourCorner(g, x, y, w, h, offsetx, offsety);
			}
			renderLimitsRect(g, x, y, offsetx, offsety);
		}
	}
	
	private void drawFourCorner(DWGraphics g) {
		if (m_is_fill) {
			g.setColor(m_background_fill);
		} else {
			g.setColor(m_background_color);
		}
		g.fillRect(m_show_x, m_show_y, m_show_w, m_show_h);
		g.drawRect(m_show_x, m_show_y, m_show_w - 1, m_show_h - 1);
		if (m_is_corner) {
			if (img_corner_lu != null) {
				g.drawBitmap(img_corner_lu, m_show_x, m_show_y, Tools.LEFT_TOP);
			}
			if (img_corner_ld != null) {
				g.drawBitmap(img_corner_ld, m_show_x, m_show_y + m_show_h, Tools.LEFT_BOTTOM);
			}
			if (img_corner_rd != null) {
				g.drawBitmap(img_corner_rd, m_show_x + m_show_w, m_show_y + m_show_h, Tools.RIGHT_BOTTOM);
			}
			if (img_corner_ru != null) {
				g.drawBitmap(img_corner_ru, m_show_x + m_show_w, m_show_y, Tools.RIGHT_TOP);
			}
		}
	}
	
	private void drawFourCorner(DWGraphics g, int x, int y, int w, int h, int offsetx, int offsety) {
		if (m_is_fill) {
			g.setColor(m_background_fill);
		} else {
			g.setColor(m_background_color);
		}
		g.fillRect(m_show_x, m_show_y, m_show_w, m_show_h);
		g.drawRect(m_show_x, m_show_y, m_show_w - 1, m_show_h - 1);
		if (m_is_corner) {
			if (img_corner_lu != null) {
				g.drawBitmap(img_corner_lu, m_show_x, m_show_y, Tools.LEFT_TOP);
			}
			if (img_corner_ld != null) {
				g.drawBitmap(img_corner_ld, m_show_x, m_show_y + m_show_h, Tools.LEFT_BOTTOM);
			}
			if (img_corner_rd != null) {
				g.drawBitmap(img_corner_rd, m_show_x + m_show_w, m_show_y + m_show_h, Tools.RIGHT_BOTTOM);
			}
			if (img_corner_ru != null) {
				g.drawBitmap(img_corner_ru, m_show_x + m_show_w, m_show_y, Tools.RIGHT_TOP);
			}
		}
	}
	public void logic()
	{
		
	}
	
	public void refreshControl()
	{
	}
	
	public boolean doScroll(PointF start, PointF end, float distanceX, float distanceY)
	{
		return false;
	}

	public boolean doClick(float x, float y)
	{
//		if (checkPointOnRect(x, y)) {
//			if (m_listener != null) {
//				m_listener.OnClick();
//				return true;
//			}
//		}
		return false;
	}

}
